﻿Imports DAL

Public Class UCGAccompagnement
    Private DefaultDate As Date = New Date(2001, 1, 1, 0, 0, 0)
    Public IsLoading As Boolean
    Private DebutPrecedent As Date = New Date(2001, 1, 1, 0, 0, 0)
    Private FinPrecedent As Date = New Date(2001, 1, 1, 0, 0, 0)
    Private DureePrecedent As Date = New Date(2001, 1, 1, 0, 0, 0)
    Private DebutPrecedentD As Date = New Date(2001, 1, 1, 0, 0, 0)
    Private FinPrecedentD As Date = New Date(2001, 1, 1, 0, 0, 0)
    Private DureePrecedentD As Date = New Date(2001, 1, 1, 0, 0, 0)
    Private TabPage2Loaded As Boolean
    Private valide As Boolean
    Private modifie As Boolean = False
    Private isprog As Boolean
    Private choixprecedent As Integer
    Private conteur As Integer
    Public Event suppression As EventHandler
    Public Event modification As EventHandler
    Public Event annulation As eventhandler

    Public Sub recharger(ByVal sender As Object, ByVal e As EventArgs)
        modifie = False
        If TabControl1.SelectedIndex = 0 Then
            If LVRécurent.SelectedItems.Count > 0 Then
                LVRécurent_SelectedIndexChanged1(Nothing, Nothing)
                RaiseEvent modification(False, Nothing)
                RaiseEvent suppression(True, Nothing)
                RaiseEvent annulation(False, Nothing)
                modifie = False
            Else
                ViderChamp(Nothing, Nothing)
                RaiseEvent suppression(False, Nothing)
                RaiseEvent modification(False, Nothing)
                RaiseEvent annulation(False, Nothing)
                modifie = False
            End If
        Else
            If LVAccompagnementD.SelectedItems.Count > 0 Then
                LVAccompagnementD_SelectedIndexChanged(Nothing, Nothing)
                RaiseEvent suppression(True, Nothing)
                RaiseEvent modification(False, Nothing)
                RaiseEvent annulation(False, Nothing)
                modifie = False
            Else
                ViderChamp(Nothing, Nothing)
                RaiseEvent suppression(False, Nothing)
                RaiseEvent modification(False, Nothing)
                RaiseEvent annulation(False, Nothing)
                modifie = False
            End If
        End If
    End Sub

    Public Sub Enregistrer(ByVal sender As Object, ByVal e As EventArgs)
        If TabControl1.SelectedIndex = 0 Then
            EnregistrerRecurrence()
        Else
            EnregistrerAccompagnement()
        End If
        RaiseEvent modification(False, Nothing)
        RaiseEvent annulation(False, Nothing)
        modifie = False
    End Sub

    Public Sub ViderChamp(ByVal sender As Object, ByVal e As EventArgs)
        If TabControl1.SelectedIndex = 0 Then
            ViderChampsReccurence()
            modifie = False
            LVRécurent.SelectedItems.Clear()
        Else
            ViderChampsAccompagnement()
            modifie = False
            LVAccompagnementD.SelectedItems.Clear()
        End If
        RaiseEvent suppression(False, Nothing)
        RaiseEvent modification(False, Nothing)
        RaiseEvent annulation(False, Nothing)
        modifie = False
    End Sub

    Public Sub Supprimer(ByVal sender As Object, ByVal e As EventArgs)
        If MsgBox("Voulez-vous vraiment supprimer cet accompagnement", vbYesNo, "Avertissement") = vbYes Then
            If TabControl1.SelectedIndex = 0 Then
                SupprimerReccurence()
            Else
                SupprimerAccompagnement()
            End If
            RaiseEvent suppression(False, Nothing)
            RaiseEvent modification(False, Nothing)
            RaiseEvent annulation(False, Nothing)
            modifier = False
        End If
    End Sub
#Region "Recurent"
    Private Sub DTPDebut_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DTPDebut.ValueChanged
        If Not Me.DesignMode Then
            If Not IsLoading Then
                IsLoading = True
                If Not DTPDebut.Value.TimeOfDay = DefaultDate.AddMinutes(1439).TimeOfDay Then
                    Dim newFin = DTPDebut.Value.AddTicks(DTPDuree.Value.TimeOfDay.Ticks)
                    If newFin.TimeOfDay <= DTPDebut.Value.TimeOfDay Then
                        DTPFin.Value = DefaultDate.AddMinutes(1439)
                        DTPDuree.Value = DefaultDate.AddTicks((DTPFin.Value.TimeOfDay - DTPDebut.Value.TimeOfDay).Ticks)
                    Else
                        DTPFin.Value = newFin
                    End If
                Else
                    DTPDebut.Value = DebutPrecedent
                End If
                IsLoading = False
            End If
            DebutPrecedent = DTPDebut.Value
        End If
    End Sub

    Private Sub DTPDuree_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DTPDuree.ValueChanged
        If Not Me.DesignMode Then
            If Not IsLoading Then
                IsLoading = True
                Dim newFin = DTPDebut.Value.AddTicks(DTPDuree.Value.TimeOfDay.Ticks)
                If newFin.TimeOfDay <= DTPDebut.Value.TimeOfDay Then
                    DTPFin.Value = DefaultDate.AddMinutes(1439)
                    DTPDuree.Value = DefaultDate.AddTicks((DTPFin.Value.TimeOfDay - DTPDebut.Value.TimeOfDay).Ticks)
                Else
                    DTPFin.Value = newFin
                End If
                IsLoading = False
            End If
        End If
    End Sub

    Private Sub DTPFin_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DTPFin.ValueChanged
        If Not Me.DesignMode Then
            If Not IsLoading Then
                IsLoading = True
                If DTPFin.Value.TimeOfDay <= DTPDebut.Value.TimeOfDay Then
                    DTPFin.Value = DTPDebut.Value.AddMinutes(1)
                Else
                    DTPDuree.Value = DefaultDate.AddTicks((DTPFin.Value.TimeOfDay - DTPDebut.Value.TimeOfDay).Ticks)
                End If
                IsLoading = False
            End If
        End If
    End Sub
    Private Sub Me_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Me.DesignMode Then
            For Each plage In (From p In bd.Recurrences Where p.Accompagnement = True Select p)
                Dim item As New ListViewItem
                item.Text = If(Not plage.Beneficiaire1 Is Nothing, plage.Beneficiaire1.Nom + ", " + plage.Beneficiaire1.Prenom, String.Empty)
                item.SubItems.Add(If(Not plage.Employe1 Is Nothing, plage.Employe1.Nom + ", " + plage.Employe1.Prenom, String.Empty))
                Select Case plage.Jour
                    Case 0
                        item.SubItems.Add("Dimanche")
                    Case 1
                        item.SubItems.Add("Lundi")
                    Case 2
                        item.SubItems.Add("Mardi")
                    Case 3
                        item.SubItems.Add("Mercredi")
                    Case 4
                        item.SubItems.Add("Jeudi")
                    Case 5
                        item.SubItems.Add("Vendredi")
                    Case 6
                        item.SubItems.Add("Samedi")
                End Select
                item.Tag = plage.ID
                LVRécurent.Items.Add(item)
            Next
            Dim employes As New Dictionary(Of Integer, String)
            For Each employe In (From i In bd.Employes Where i.Supprime = False Select i)
                employes.Add(employe.ID, employe.Nom + ", " + employe.Prenom)
            Next
            If employes.Count > 0 Then
                CBEmploye.DataSource = New BindingSource(employes, Nothing)
                CBEmploye.ValueMember = "Key"
                CBEmploye.DisplayMember = "Value"
            End If

            Dim beneficiaires As New Dictionary(Of Integer, String)
            For Each beneficiaire In (From i In bd.Beneficiaires Where i.Supprime = False Select i)
                beneficiaires.Add(beneficiaire.ID, beneficiaire.Nom + ", " + beneficiaire.Prenom)
            Next
            If beneficiaires.Count > 0 Then
                CBBeneficiaire.DataSource = New BindingSource(beneficiaires, Nothing)
                CBBeneficiaire.ValueMember = "Key"
                CBBeneficiaire.DisplayMember = "Value"
            End If

            Dim jours As New Dictionary(Of Integer, String)
            jours.Add(0, "Dimanche")
            jours.Add(1, "Lundi")
            jours.Add(2, "Mardi")
            jours.Add(3, "Mercredi")
            jours.Add(4, "Jeudi")
            jours.Add(5, "Vendredi")
            jours.Add(6, "Samedi")
            CBJour.DataSource = New BindingSource(jours, Nothing)
            CBJour.ValueMember = "Key"
            CBJour.DisplayMember = "Value"
            If LVRécurent.Items.Count > 0 Then
                LVRécurent.Items(0).Selected = True
            End If
        End If
        modifie = False
        RaiseEvent modification(False, Nothing)
        RaiseEvent annulation(False, Nothing)
    End Sub

    Public Sub EnregistrerRecurrence()
        valide = True
        ErrorProvider1.Clear()
        If CBBeneficiaire.SelectedValue = Nothing OrElse CBBeneficiaire.SelectedValue = -1 Then
            ErrorProvider1.SetError(CBBeneficiaire, "Vous devez choisir un bénéficiaire")
            valide = False
        End If
        If CBEmploye.SelectedValue = Nothing OrElse CBEmploye.SelectedValue = -1 Then
            ErrorProvider1.SetError(CBEmploye, "Vous devez choisir un employé")
            valide = False
        End If
        If DTPDuree.Value.TimeOfDay.TotalHours = 0 And DTPDuree.Value.TimeOfDay.TotalMinutes = 0 Then
            ErrorProvider1.SetError(DTPDuree, "La plage doit avoir une durée")
            valide = False
        End If
        If CBJour.SelectedIndex = -1 Then
            ErrorProvider1.SetError(CBJour, "Vous devez choisir une journée")
            valide = False
        End If
        If valide Then
            Dim Accompagnement As Recurrence
            If LVRécurent.SelectedIndices.Count > 0 Then
                Accompagnement = (From i In bd.Recurrences Where i.ID = Convert.ToInt32(LVRécurent.SelectedItems(0).Tag) Select i).First
            Else
                Accompagnement = New Recurrence
            End If
            With Accompagnement
                .Accompagnement = True
                .Beneficiaire1 = (From b In bd.Beneficiaires Where b.ID = Convert.ToInt32(CBBeneficiaire.SelectedValue) Select b).First
                .Durée = DTPDuree.Value.TimeOfDay.TotalMinutes
                .Employe1 = (From emp In bd.Employes Where emp.ID = Convert.ToInt32(CBEmploye.SelectedValue) Select emp).First
                .Heure = DTPDebut.Value.TimeOfDay
                .Jour = CBJour.SelectedValue
                .Couleur = (From i In bd.Parametres Select i.CouleurAccompagnement).First
                .Nom = "Accompagnement"
                .Note = TBNote.Text
            End With
            If LVRécurent.SelectedItems.Count = 0 Then
                bd.Recurrences.InsertOnSubmit(Accompagnement)
            End If
            bd.SubmitChanges()
            If LVRécurent.SelectedIndices.Count = 0 Then
                Dim item As New ListViewItem
                item.Text = Accompagnement.Beneficiaire1.Nom + ", " + Accompagnement.Beneficiaire1.Prenom
                item.SubItems.Add(Accompagnement.Employe1.Nom + ", " + Accompagnement.Employe1.Prenom)
                Select Case Accompagnement.Jour
                    Case 0
                        item.SubItems.Add("Dimanche")
                    Case 1
                        item.SubItems.Add("Lundi")
                    Case 2
                        item.SubItems.Add("Mardi")
                    Case 3
                        item.SubItems.Add("Mercredi")
                    Case 4
                        item.SubItems.Add("Jeudi")
                    Case 5
                        item.SubItems.Add("Vendredi")
                    Case 6
                        item.SubItems.Add("Samedi")
                End Select
                item.Tag = Accompagnement.ID
                LVRécurent.Items.Add(item)
            Else
                Dim item As ListViewItem = LVRécurent.SelectedItems(0)
                item.Text = Accompagnement.Beneficiaire1.Nom + ", " + Accompagnement.Beneficiaire1.Prenom
                item.SubItems.Add(Accompagnement.Employe1.Nom + ", " + Accompagnement.Employe1.Prenom)
                Select Case Accompagnement.Jour
                    Case 0
                        item.SubItems.Add("Dimanche")
                    Case 1
                        item.SubItems.Add("Lundi")
                    Case 2
                        item.SubItems.Add("Mardi")
                    Case 3
                        item.SubItems.Add("Mercredi")
                    Case 4
                        item.SubItems.Add("Jeudi")
                    Case 5
                        item.SubItems.Add("Vendredi")
                    Case 6
                        item.SubItems.Add("Samedi")
                End Select
                item.Tag = Accompagnement.ID
            End If
            modifie = False
            LVRécurent.Items(0).Selected = True
        End If
    End Sub

    Public Sub ViderChampsReccurence()
        CBBeneficiaire.SelectedIndex = -1
        CBEmploye.SelectedIndex = -1
        CBJour.SelectedIndex = -1
        IsLoading = True
        DTPDebut.Value = DefaultDate
        DTPFin.Value = DefaultDate.AddHours(1)
        DTPDuree.Value = DefaultDate.AddHours(1)
        IsLoading = False
        TBNote.Text = String.Empty
    End Sub

    Public Sub SupprimerReccurence()
        bd.Recurrences.DeleteOnSubmit((From r In bd.Recurrences Where r.ID = Convert.ToInt32(LVRécurent.SelectedItems(0).Tag)).First)
        bd.SubmitChanges()
        LVRécurent.Items.Remove(LVRécurent.SelectedItems(0))
        If LVRécurent.Items.Count > 0 Then
            LVRécurent.Items(0).Selected = True
        End If
    End Sub

    Private Sub LVRécurent_SelectedIndexChanged(ByVal sender As Object, ByVal e As ItemChangingEventArgs) Handles LVRécurent.ItemDeselecting
        If Not modifie Then

        Else
            MsgBox("Vous devez sauvegarder")
            e.Cancel = True
        End If
    End Sub
#End Region
#Region "Non-Recurent"
    Private Sub DTPDebutD_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DTPDebutD.ValueChanged
        If Not Me.DesignMode Then
            If Not IsLoading Then
                IsLoading = True
                If Not DTPDebutD.Value.TimeOfDay = DefaultDate.AddMinutes(1439).TimeOfDay Then
                    Dim newFin = DTPDebutD.Value.AddTicks(DTPDureeD.Value.TimeOfDay.Ticks)
                    If newFin.TimeOfDay <= DTPDebutD.Value.TimeOfDay Then
                        DTPFinD.Value = DefaultDate.AddMinutes(1439)
                        DTPDureeD.Value = DefaultDate.AddTicks((DTPFinD.Value.TimeOfDay - DTPDebutD.Value.TimeOfDay).Ticks)
                    Else
                        DTPFinD.Value = newFin
                    End If
                Else
                    DTPDebutD.Value = DebutPrecedentD
                End If
                IsLoading = False
            End If
            DebutPrecedentD = DTPDebutD.Value
        End If
    End Sub

    Private Sub DTPDureeD_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DTPDureeD.ValueChanged
        If Not Me.DesignMode Then
            If Not IsLoading Then
                IsLoading = True
                Dim newFin = DTPDebutD.Value.AddTicks(DTPDureeD.Value.TimeOfDay.Ticks)
                If newFin.TimeOfDay <= DTPDebutD.Value.TimeOfDay Then
                    DTPFinD.Value = DefaultDate.AddMinutes(1439)
                    DTPDureeD.Value = DefaultDate.AddTicks((DTPFinD.Value.TimeOfDay - DTPDebutD.Value.TimeOfDay).Ticks)
                Else
                    DTPFinD.Value = newFin
                End If
                IsLoading = False
            End If
        End If
    End Sub

    Private Sub DTPFinD_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DTPFinD.ValueChanged
        If Not Me.DesignMode Then
            If Not IsLoading Then
                IsLoading = True
                If DTPFinD.Value.TimeOfDay <= DTPDebutD.Value.TimeOfDay Then
                    DTPFinD.Value = DTPDebutD.Value.AddMinutes(1)
                Else
                    DTPDureeD.Value = DefaultDate.AddTicks((DTPFinD.Value.TimeOfDay - DTPDebutD.Value.TimeOfDay).Ticks)
                End If
                IsLoading = False
            End If
        End If
    End Sub

    Private Sub TabControl1_TabIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TabControl1.SelectedIndexChanged
        If Not modifie Then
            RaiseEvent suppression(False, Nothing)
            If Not Me.DesignMode Then
                If TabControl1.SelectedIndex = 1 Then
                    If Not TabPage2Loaded Then
                        TabPage2Loaded = True
                        For Each plage In (From p In bd.PlageHoraires Where p.Accompagnement = True And p.Date > Date.Now Select p)
                            Dim item As New ListViewItem
                            item.Text = If(Not plage.Beneficiaire1 Is Nothing, plage.Beneficiaire1.Nom + ", " + plage.Beneficiaire1.Prenom, String.Empty)
                            item.SubItems.Add(If(Not plage.Employe1 Is Nothing, plage.Employe1.Nom + ", " + plage.Employe1.Prenom, String.Empty))
                            item.SubItems.Add(plage.Date.ToShortDateString)
                            item.Tag = plage.ID
                            LVAccompagnementD.Items.Add(item)
                        Next
                        Dim employesD As New Dictionary(Of Integer, String)
                        For Each employe In (From i In bd.Employes Where i.Supprime = False Select i)
                            employesD.Add(employe.ID, employe.Nom + ", " + employe.Prenom)
                        Next
                        If employesD.Count > 0 Then
                            CBEmployeD.DataSource = New BindingSource(employesD, Nothing)
                            CBEmployeD.ValueMember = "Key"
                            CBEmployeD.DisplayMember = "Value"
                        End If

                        Dim beneficiairesD As New Dictionary(Of Integer, String)
                        For Each beneficiaire In (From i In bd.Beneficiaires Where i.Supprime = False Select i)
                            beneficiairesD.Add(beneficiaire.ID, beneficiaire.Nom + ", " + beneficiaire.Prenom)
                        Next
                        If beneficiairesD.Count > 0 Then
                            CBBeneficiaireD.DataSource = New BindingSource(beneficiairesD, Nothing)
                            CBBeneficiaireD.ValueMember = "Key"
                            CBBeneficiaireD.DisplayMember = "Value"
                        End If
                        DTPDate.Value = Today
                        modifie = False
                        If LVAccompagnementD.Items.Count > 0 Then
                            LVAccompagnementD.Items(0).Selected = True
                        End If
                    Else
                        If LVAccompagnementD.Items.Count > 0 Then
                            LVAccompagnementD.Items(0).Selected = True
                        End If
                    End If
                    RaiseEvent modification(False, Nothing)
                    RaiseEvent annulation(False, Nothing)
                Else
                    If LVRécurent.Items.Count > 0 Then
                        LVRécurent.Items(0).Selected = True
                    End If
                End If
            End If
            modifie = False
            choixprecedent = TabControl1.SelectedIndex
        Else
            If Not IsLoading Then
                IsLoading = True
                TabControl1.SelectedIndex = choixprecedent
                MsgBox("Vous devez sauvegarder avant de changer")
                IsLoading = False
            End If
        End If
    End Sub

    Public Sub EnregistrerAccompagnement()
        valide = True
        ErrorProvider1.Clear()
        If CBBeneficiaireD.SelectedValue = Nothing OrElse CBBeneficiaireD.SelectedValue = -1 Then
            ErrorProvider1.SetError(CBBeneficiaireD, "Vous devez choisir un bénéficiaire")
            valide = False
        End If
        If CBEmployeD.SelectedValue = Nothing OrElse CBEmployeD.SelectedValue = -1 Then
            ErrorProvider1.SetError(CBEmployeD, "Vous devez choisir un employé")
            valide = False
        End If
        If DTPDureeD.Value.TimeOfDay.TotalHours = 0 And DTPDureeD.Value.TimeOfDay.TotalMinutes = 0 Then
            ErrorProvider1.SetError(DTPDureeD, "La plage doit avoir une durée")
            valide = False
        End If
        If DTPDate.Value < Date.Now Then
            If MsgBox("La date choisie est antérieur à la date d'aujourd'hui. Voulez-vous vraiment sauvegarder à cette date?", vbYesNo, "Avertissement") = vbNo Then
                ErrorProvider1.SetError(DTPDate, "La date est antérieure à la date d’aujourd'hui")
                valide = False
            End If
        End If
        If valide Then
            Dim Accompagnement As PlageHoraire
            If LVAccompagnementD.SelectedIndices.Count > 0 Then
                Accompagnement = (From i In bd.PlageHoraires Where i.ID = Convert.ToInt32(LVAccompagnementD.SelectedItems(0).Tag) Select i).First
            Else
                Accompagnement = New PlageHoraire
            End If
            With Accompagnement
                .Accompagnement = True
                .Beneficiaire1 = (From b In bd.Beneficiaires Where b.ID = Convert.ToInt32(CBBeneficiaireD.SelectedValue) Select b).First
                .Duree = DTPDureeD.Value.TimeOfDay.TotalMinutes
                .Employe1 = (From emp In bd.Employes Where emp.ID = Convert.ToInt32(CBEmployeD.SelectedValue) Select emp).First
                .Date = DTPDate.Value.AddMinutes(DTPDebutD.Value.TimeOfDay.TotalMinutes)
                If (From i In bd.Parametres Select i.CouleurAccompagnement).Count > 0 Then
                    .Couleur = (From i In bd.Parametres Select i.CouleurAccompagnement).First
                Else
                    .Couleur = "#dddddd"
                End If

                .Nom = "Accompagnement"
                .Note = TBNoteD.Text
            End With
            If LVAccompagnementD.SelectedIndices.Count = 0 Then
                bd.PlageHoraires.InsertOnSubmit(Accompagnement)
            End If
            bd.SubmitChanges()
            If LVAccompagnementD.SelectedIndices.Count = 0 Then
                Dim item As New ListViewItem
                item.Text = Accompagnement.Beneficiaire1.Nom + ", " + Accompagnement.Beneficiaire1.Prenom
                item.SubItems.Add(Accompagnement.Employe1.Nom + ", " + Accompagnement.Employe1.Prenom)
                item.SubItems.Add(Accompagnement.Date)
                item.Tag = Accompagnement.ID
                LVAccompagnementD.Items.Add(item)
            Else
                Dim item As ListViewItem = LVAccompagnementD.SelectedItems(0)
                item.Text = Accompagnement.Beneficiaire1.Nom + ", " + Accompagnement.Beneficiaire1.Prenom
                item.SubItems.Add(Accompagnement.Employe1.Nom + ", " + Accompagnement.Employe1.Prenom)
                item.SubItems.Add(Accompagnement.Date)
                item.Tag = Accompagnement.ID
            End If
            modifie = False
            LVAccompagnementD.Items(0).Selected = True
        End If
    End Sub

    Public Sub ViderChampsAccompagnement()
        CBBeneficiaireD.SelectedIndex = -1
        CBEmployeD.SelectedIndex = -1
        DTPDate.Value = Today
        IsLoading = True
        DTPDebutD.Value = DefaultDate
        DTPFinD.Value = DefaultDate.AddHours(1)
        DTPDureeD.Value = DefaultDate.AddHours(1)
        IsLoading = False
        TBNoteD.Text = String.Empty
    End Sub

    Public Sub SupprimerAccompagnement()
        bd.PlageHoraires.DeleteOnSubmit((From r In bd.PlageHoraires Where r.ID = Convert.ToInt32(LVAccompagnementD.SelectedItems(0).Tag)).First)
        bd.SubmitChanges()
        LVAccompagnementD.Items.Remove(LVAccompagnementD.SelectedItems(0))
        If LVAccompagnementD.Items.Count > 0 Then
            LVAccompagnementD.Items(0).Selected = True
        End If
    End Sub

    Private Sub LVAccompagnement_SelectedIndexChanged(ByVal sender As Object, ByVal e As ItemChangingEventArgs) Handles LVAccompagnementD.ItemDeselecting
        If Not modifie Then

        Else
            e.Cancel = True
            MsgBox("Vous devez sauvegarder avant de changer d'accompagnement")
        End If
    End Sub
#End Region

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click, BTListeD.Click
        If TabControl1.SelectedIndex = 0 Then
            If Not CBBeneficiaire.SelectedValue = Nothing AndAlso Not CBBeneficiaire.SelectedValue = -1 Then
                ListeEmployeAffinite.ControlAppel = CBEmploye
                ListeEmployeAffinite.beneficiaireSelectionne = (From i In bd.Beneficiaires Where i.ID = Convert.ToInt32(CBBeneficiaire.SelectedValue) Select i).First
                ListeEmployeAffinite.ShowDialog()
            End If
        Else
            If Not CBBeneficiaireD.SelectedValue = Nothing AndAlso Not CBBeneficiaireD.SelectedValue = -1 Then
                ListeEmployeAffinite.ControlAppel = CBEmployeD
                ListeEmployeAffinite.beneficiaireSelectionne = (From i In bd.Beneficiaires Where i.ID = Convert.ToInt32(CBBeneficiaireD.SelectedValue) Select i).First
                ListeEmployeAffinite.ShowDialog()
            End If
        End If
    End Sub


    Private Sub CBBeneficiaire_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CBBeneficiaire.SelectedIndexChanged, CBBeneficiaireD.SelectedIndexChanged, CBEmploye.SelectedIndexChanged, CBEmployeD.SelectedIndexChanged, CBJour.SelectedIndexChanged, DTPDate.ValueChanged, DTPDebut.ValueChanged, DTPDebutD.ValueChanged, DTPDuree.ValueChanged, DTPDureeD.ValueChanged, DTPFin.ValueChanged, DTPFinD.ValueChanged, TBNoteD.TextChanged, TBNote.TextChanged, DTPDate.ValueChanged
        RaiseEvent modification(True, Nothing)
        RaiseEvent annulation(True, Nothing)
        modifie = True
    End Sub

    Private Sub LVRécurent_SelectedIndexChanged1(ByVal sender As Object, ByVal e As System.EventArgs) Handles LVRécurent.SelectedIndexChanged
        If LVRécurent.SelectedItems.Count > 0 Then
            Dim accompagnement = (From r In bd.Recurrences Where r.ID = Convert.ToInt32(LVRécurent.SelectedItems(0).Tag) Select r).First
            With accompagnement
                CBBeneficiaire.SelectedValue = .Beneficiaire
                DTPDuree.Value = DefaultDate.AddMinutes(.Durée)
                CBEmploye.SelectedValue = .Employe
                DTPDebut.Value = DefaultDate.AddMinutes(.Heure.Value.TotalMinutes)
                CBJour.SelectedValue = .Jour
                TBNote.Text = .Note
            End With
            RaiseEvent suppression(True, Nothing)
        Else
            RaiseEvent suppression(False, Nothing)
        End If
        modifie = False
        RaiseEvent modification(False, Nothing)
        RaiseEvent annulation(False, Nothing)
    End Sub

    Private Sub LVAccompagnementD_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles LVAccompagnementD.SelectedIndexChanged
        If LVAccompagnementD.SelectedItems.Count > 0 Then
            Dim accompagnement = (From r In bd.PlageHoraires Where r.ID = Convert.ToInt32(LVAccompagnementD.SelectedItems(0).Tag) Select r).First
            With accompagnement
                CBBeneficiaireD.SelectedValue = .Beneficiaire
                DTPDureeD.Value = DefaultDate.AddMinutes(.Duree)
                CBEmployeD.SelectedValue = .Employe
                DTPDebutD.Value = DefaultDate.AddMinutes(.Date.TimeOfDay.TotalMinutes)
                DTPDate.Value = .Date.Date
                TBNoteD.Text = .Note
            End With
            RaiseEvent suppression(True, Nothing)
            RaiseEvent modification(False, Nothing)
            RaiseEvent annulation(False, Nothing)
            LVAccompagnementD.Enabled = True
        Else
            RaiseEvent suppression(False, Nothing)
        End If
    End Sub

    Private Sub LVAccompagnementD_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub
End Class
